//
//  BaseServiceManager.m
//  IMDome
//
//  Created by limaohuyu on 2020/8/13.
//  Copyright © 2020 czw. All rights reserved.
//

#import "BaseServiceManager.h"

@implementation BaseServiceManager
NSString *const agent = @"Mobile+iPhone";

+ (AFHTTPSessionManager *)createSessionManager{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/json;charset=utf-8;application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    //设置请求头
           [manager.requestSerializer setValue:@"tokenDDD"forHTTPHeaderField:@"token"];

    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 60;
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", @"multipart/form-data", @"application/json", @"text/html", @"application/octet-stream", @"text/json", @"text/javascript", nil];
    WGLog(@"HTTPRequestHeaders------->>>>>%@",manager.requestSerializer.HTTPRequestHeaders);
    return manager;
    
}


+(void)postRequest:(NSString *)postUrl
            params:(NSDictionary*)params
        startBlock:(VoidBlock)startBlock
      successBlock:(SuccessBlock)successBlock
      failureBlock:(FailureBlock)failureBlock
{
    
    AFHTTPSessionManager *manager = [self createSessionManager];
    
    NSString  * url = [NSString stringWithFormat:@"%@%@",BASE_URL,postUrl];
    WGLog(@"请求数据-content--->>>%@\n---------->>> 请求RUL---%@",params,url);
    [manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        if (startBlock) {
            startBlock();
        }
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSObject *responseTemp = responseObject;
        WGLog(@"请求RUL---------->>>%@\n返回的数据----->>>>>%@",url,responseTemp.mj_JSONString);
         ResponseBaseModel *response =   [ResponseBaseModel mj_objectWithKeyValues:responseObject];
        if (response.status == 200) {
            if (successBlock) {
                successBlock(response);
            }
        }else{
            NSError *error;
            if (failureBlock) {
                failureBlock(error,responseObject[@"msg"]);
            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failureBlock) {
            NSLog(@"=====%@",url);
            failureBlock(error,@"数据出错了，请稍后尝试");
        }
    }];
}

+(void)getRequest:(NSString *)getUrl
           params:(NSDictionary*)params
       startBlock:(VoidBlock)startBlock
     successBlock:(SuccessBlock)successBlock
     failureBlock:(FailureBlock)failureBlock{
    
    AFHTTPSessionManager *manager = [self createSessionManager];
    NSString  * url = [NSString stringWithFormat:@"%@%@",BASE_URL,getUrl];
    WGLog(@"请求数据content----->>>%@\n请求RUL---------->>>%@",params,url);
    if (startBlock) {
        startBlock();
    }
    [manager GET: [self getRequest:url parameters:params] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         NSObject *responseTemp = responseObject;
           WGLog(@"请求RUL---------->>>%@\n返回的数据----->>>>>%@",url,responseTemp.mj_JSONString);
        ResponseBaseModel *response =   [ResponseBaseModel mj_objectWithKeyValues:responseObject];
        if (response.status == 200) {
            if (successBlock) {
                successBlock(response);
            }
        }else{
            NSError *error;
            if (failureBlock) {
                failureBlock(error,responseObject[@"msg"]);
            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failureBlock) {
            failureBlock(error,@"数据出错了，请稍后尝试");
        }
    }];
}



+ (NSString *)getRequest:(NSString *)getUrl parameters:(NSDictionary *)parameters {
    if ([parameters allKeys].count == 0) {
        return getUrl;
    }
    NSMutableString *mutableUrl = [[NSMutableString alloc] initWithString:getUrl];
    if ([parameters allKeys]) {
        [mutableUrl appendString:@"?"];
        for (id key in parameters) {
            NSString *value = [[parameters objectForKey:key] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
            [mutableUrl appendString:[NSString stringWithFormat:@"%@=%@&", key, value]];
        }
    }
    NSString *urlEnCode = [[mutableUrl substringToIndex:mutableUrl.length - 1] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    return urlEnCode;
}
@end
